package B_Splain;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public class GraphPanel extends JPanel {
    List<Point2D> pointList = new LinkedList<>();// 鼠标点击的所有节点

    List<List<Point2D>> partList = new ArrayList<>();// 鼠标点击的所有节点
//    List<Point2D> curvePoints = new ArrayList<>();//曲线上的点

    List<Point2D> tempPoints;//通过书记面板选中的点

    List longestCurve = new LinkedList<Point2D>();


    Point2D currenPpoint = null;
    int radius = 0;
    public GraphPanel() {
        // 设置边框为黑色线条边框
        setBorder(BorderFactory.createLineBorder(Color.WHITE));
        // 设置首选大小为200x150
        setPreferredSize(new Dimension(200, 150));
        // 设置背景颜色为白色
        setBackground(Color.WHITE);
        // 设置面板为不透明
        setOpaque(true);
        // 设置面板为可见
        setVisible(true);
    }



    public void paintPoint(Graphics2D g,Color color,Point2D point,int radius) {
        g.setColor(color); // 设置点的颜色
        currenPpoint = point;
        this.radius = radius;
        g.fillOval( (int) point.getX() - 2, (int) point.getY() - 2, radius, radius); // 绘制一个小的圆形表示点
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 遍历点列表并绘制每个点及其标签
        for (List<Point2D> point2DS : partList) {
            for (int i = 0; i < point2DS.size(); i++) {

                Point2D point2D = point2DS.get(i);
                g.setColor(Color.BLACK);
                g.drawOval((int) point2D.getX(), (int) point2D.getY(), 2, 2);
                g.drawString("(" + point2D.getX() + " , " + point2D.getY() + ")",
                        (int) point2D.getX() + 5, (int) point2D.getY() - 5); // 调整位置以避免重叠

                // 每两个点用直线连接起
                if (i != 0) {
                    Point2D point2DLast = point2DS.get(i - 1);
                    g.drawLine((int) point2D.getX(), (int) point2D.getY(),
                            (int) point2DLast.getX(), (int) point2DLast.getY());
                }
            }
            // 绘制形成的曲线
            ArrayList<Point2D> curvePoints = computCurvePoints(point2DS);
            g.setColor(Color.RED); // 设置点的颜色
            for (Point2D p : curvePoints) {
                g.fillOval((int) (p.getX() - 2), (int) (p.getY() - 2), 4, 4); // 绘制一个小的圆形表示点
            }

        }

        // 绘制额外的点集
//        g.setColor(Color.RED); // 设置点的颜色
//        for (Point2D p : curvePoints) {
//            g.fillOval((int) (p.getX() - 2), (int) (p.getY() - 2), 4, 4); // 绘制一个小的圆形表示点
//        }

//        // 绘制当前点
//        if (currenPpoint != null) {
//            g.setColor(Color.RED); // 设置点的颜色
//            g.fillOval((int) (currenPpoint.getX() - 2), (int) (currenPpoint.getY() - 2), radius, radius); // 绘制一个小的圆形表示点
//        }

        //绘制选中点
        if (tempPoints != null) {
            g.setColor(new Color(255, 0, 0)); // 设置点的颜色
            for (Point2D p : tempPoints) {
                g.fillOval((int) (p.getX() - 2), (int) (p.getY() - 2), 7, 7); // 绘制一个小的圆形表示点
            }
        }

    }

    public void paintCurve(Graphics2D g) throws InterruptedException {
        computCurvePoints(pointList);
    }

    public ArrayList<Point2D> computCurvePoints(List<Point2D> points) {
        ArrayList<Point2D> curvePoints = new ArrayList<>();
        for (int i = 0; i < points.size() - 3; i++) {
            for (double t = 0; t <= 1; t += 0.001) {
                double b0 = (1 - t) * (1 - t) * (1 - t) / 6;
                double b1 = (3 * t * t * t - 6 * t * t + 4) / 6;
                double b2 = (-3 * t * t * t + 3 * t * t + 3 * t + 1) / 6;
                double b3 = t * t * t / 6;
                double x = b0 * points.get(i).getX() + b1 * points.get(i + 1).getX()
                        + b2 * points.get(i + 2).getX() + b3 * points.get(i + 3).getX();
                double y = b0 * points.get(i).getY() + b1 * points.get(i + 1).getY()
                        + b2 * points.get(i + 2).getY() + b3 * points.get(i + 3).getY();

                curvePoints.add(new Point2D.Double(x, y));
            }
        }
//        this.curvePoints = curvePoints;
        return curvePoints;
    }
}
